<?php

namespace App\Http\Controllers\Admin;

use App\Modules\Order\Models\Order;
use App\Modules\Rebate\Models\Rebate;
use App\Modules\User\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class TeamController extends Model
{
    public function index()
    {
        $user = Auth::user();
        $achievement = Order::query()
            ->where('personal_id', $user->id)
            ->where('status', Order::STATUS_PAID)
            ->sum('price');

        $team = Rebate::query()
            ->join('users', 'users.id', '=', 'rebates.user_id')
            ->where('users.personal_id', $user->id)
            ->where('rebates.status', Rebate::STATUS_ISSUED)
            ->selectRaw('sum(money) as money, user_id, users.nickname, users.avatar')
            ->groupBy('user_id')
            ->get();
        $teamIncome = $team->sum('money');
        $share = User::query()
            ->where('personal_id', $user->id)
            ->sum('share');

        return view('team.index', compact('data', 'achievement', 'teamIncome', 'share', 'team'));
    }
}
